package service.examinfo;

import java.util.List;

import service.notice.NoticeService;
import util.manager.MajorunitManager;
import util.manager.PageManager;
import util.paramvo.PageListParamVO;
import util.returnvo.ExaminfoListReturnVO;
import vo.ExaminfoVO;
import vo.MajorunitVO;
import config.PageConfig;
import dao.examinfo.ExaminfoDAO;

public class ExaminfoServiceImpl implements ExaminfoService{

	private ExaminfoDAO examinfoDAO;
	private NoticeService noticeService;

	public void setExaminfoDAO(ExaminfoDAO examinfoDAO) {
		this.examinfoDAO = examinfoDAO;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	@Override
	public ExaminfoListReturnVO getUnapprovalExaminfoList(String page) throws Exception {
		
		int pageNo=1;
		
		if(page!=null && !page.equals("")) pageNo=Integer.parseInt(page);
		
		List<ExaminfoVO> examinfoList=examinfoDAO.getUnapprovalExaminfoList(new PageListParamVO(PageConfig.EXAMINFO_ROW_PER_PAGE, pageNo));
		
		return new ExaminfoListReturnVO(examinfoList, new PageManager(PageConfig.EXAMINFO_ROW_PER_PAGE, PageConfig.EXAMINFO_PAGE_PER_GROUP, examinfoDAO.countUnapprovalExaminfoList(), pageNo));
	}

	@Override
	public void approveExaminfo(int examinfo_no) throws Exception {
		examinfoDAO.approveExaminfo(new ExaminfoVO(examinfo_no, 1));
		
		ExaminfoVO examinfo=examinfoDAO.getExaminfo(examinfo_no);
		MajorunitVO majorunit=examinfo.getMajorunit();
		noticeService.toInterestedMember(
				majorunit.getMajorunit_no(), 
				"새 입시정보", 
				"<a href='review.do?command=review&&majorunit_no="+majorunit.getMajorunit_no()+"'>"+MajorunitManager.convertUnitToString(majorunit)+"</a>에 새 입시 정보가 등록되었습니다.");
		
		noticeService.sendNotice(
				examinfo.getMember().getMember_no(), 
				"승인 완료", 
				"<a href='review.do?command=review&&majorunit_no="+majorunit.getMajorunit_no()+"'>"+MajorunitManager.convertUnitToString(majorunit)+"</a>에 작성하신 "+examinfo.getExaminfo_title()+" 글이 승인 완료되었습니다.");
	}

	@Override
	public void refuseExaminfo(int examinfo_no, String reason) throws Exception {
		examinfoDAO.approveExaminfo(new ExaminfoVO(examinfo_no, -1));
		
		ExaminfoVO examinfo=examinfoDAO.getExaminfo(examinfo_no);
		MajorunitVO majorunit=examinfo.getMajorunit();
		noticeService.sendNotice(
				examinfo.getMember().getMember_no(), 
				"승인 불가", 
				"<a href='review.do?command=review&&majorunit_no="+majorunit.getMajorunit_no()+"'>"+MajorunitManager.convertUnitToString(majorunit)+"</a>에 작성하신 "+examinfo.getExaminfo_title()+" 글이 승인 불가 판정을 받았습니다.<br>"+
				"승인 불가이유 : <br>"+reason);
	}
}
